package com.example.prom.service;

import com.example.prom.annotation.CounterMetricAnnotation;
import com.example.prom.annotation.TimerMetricAnnotation;
import com.example.prom.model.Message;
import com.example.prom.utils.RandomUtil;
import com.example.prom.vo.OrderVO;
import io.micrometer.core.instrument.Metrics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class OrderService {

    @Autowired
    private MessageService messageService;

    @CounterMetricAnnotation(name = "bafang.common.sms", tags = {"channel", "mengwang"})
    @TimerMetricAnnotation(name = "bafang.common.sms", tags = {"channel", "mengwang"})
    public Boolean createOrder1(OrderVO order) {
        //模拟下单
        try {
            int ms = RandomUtil.nextMs(600);
            TimeUnit.MILLISECONDS.sleep(ms);
            log.info("保存订单模拟耗时{}毫秒...", ms);
        } catch (Exception e) {
            //no-op
        }
        //记录下单总数
        Metrics.counter("order.count", "order.channel", order.getChannel()).increment();
        //发送消息
        Message message = new Message();
        message.setContent("模拟短信...");
        message.setOrderId(order.getOrderId());
        message.setUserId(order.getUserId());
        messageService.sendMessage(message);
        return RandomUtil.randomBool(10, 3);
    }
    @CounterMetricAnnotation(name = "bafang.common.sms", tags = {"channel", "shouwang"})
    @TimerMetricAnnotation(name = "bafang.common.sms", tags = {"channel", "shouwang"})
    public Boolean createOrder2(OrderVO order) {
        //模拟下单
        try {
            int ms = RandomUtil.nextMs(500);
            TimeUnit.MILLISECONDS.sleep(ms);
            log.info("保存订单模拟耗时{}毫秒...", ms);
        } catch (Exception e) {
            //no-op
        }
        //记录下单总数
        Metrics.counter("order.count", "order.channel", order.getChannel()).increment();
        //发送消息
        Message message = new Message();
        message.setContent("模拟短信...");
        message.setOrderId(order.getOrderId());
        message.setUserId(order.getUserId());
        messageService.sendMessage(message);
        return RandomUtil.randomBool(5, 2);
    }
    @CounterMetricAnnotation(name = "bafang.common.sms", tags = {"channel", "jumeng"})
    @TimerMetricAnnotation(name = "bafang.common.sms", tags = {"channel", "jumeng"})
    public Boolean createOrder3(OrderVO order) {
        //模拟下单
        try {
            int ms = RandomUtil.nextMs(400);
            TimeUnit.MILLISECONDS.sleep(ms);
            log.info("保存订单模拟耗时{}毫秒...", ms);
        } catch (Exception e) {
            //no-op
        }
        //记录下单总数
        Metrics.counter("order.count", "order.channel", order.getChannel()).increment();
        //发送消息
        Message message = new Message();
        message.setContent("模拟短信...");
        message.setOrderId(order.getOrderId());
        message.setUserId(order.getUserId());
        messageService.sendMessage(message);
        return RandomUtil.randomBool(4, 3);
    }
    @CounterMetricAnnotation(name = "bafang.common.sms", tags = {"channel", "twilio"})
    @TimerMetricAnnotation(name = "bafang.common.sms", tags = {"channel", "twilio"})
    public Boolean createOrder4(OrderVO order) {
        //模拟下单
        try {
            int ms = RandomUtil.nextMs(300);
            TimeUnit.MILLISECONDS.sleep(ms);
            log.info("保存订单模拟耗时{}毫秒...", ms);
        } catch (Exception e) {
            //no-op
        }
        //记录下单总数
        Metrics.counter("order.count", "order.channel", order.getChannel()).increment();
        //发送消息
        Message message = new Message();
        message.setContent("模拟短信...");
        message.setOrderId(order.getOrderId());
        message.setUserId(order.getUserId());
        messageService.sendMessage(message);
        return RandomUtil.randomBool(6, 2);
    }

}
